<template>
	<div class="login">
		<div class="top">
			<image :src="obtainImgUrl('icon/icon_login_back.jpg')" mode="widthFix"></image>
		</div>
		<div class="content">
			<div class="line">
				<label>+86</label>
				<p><input type="text" placeholder="请输入手机号码" placeholder-class="placeholder" v-model="mobileLogin.mobile" /></p>
			</div>
			<div class="line">
				<label>验证码</label>
				<p>
					<input type="text" placeholder="请输入验证码" placeholder-class="placeholder" v-model="mobileLogin.code" />
					<span @click="sendCode()">{{sendCodeTxt}}</span>
				</p>
			</div>
			<button @click="login()" >立即登录</button>
			
			    <div class="other">
			        <view class="maskBtn" v-if="!isCheckRule" @click="bindCheckRule(1)"></view>
			        <button type="default" open-type='getPhoneNumber' @getphonenumber="getPhoneNumber" style="background: #fff;width: 100%; margin: 20px 0; height:60px;">
			            <image v-if="envVersion == 'release'" :src="obtainImgUrl('icon/icon_login_wechat.png')"></image>
			            <text v-else>手机号授权快捷登录</text>
			        </button>
			    </div>
			<p class="rule">
				<i :class="isCheckRule ? 'check' : ''" @click="bindCheckRule(0)"></i>
				<span @click="bindCheckRule(0)">阅读并同意以下协议</span>
				<b @click="jump('agreement', 'register')">《服务协议》</b> <b @click="jump('agreement', 'privacy')">《隐私协议》</b>
			</p>
			<p style="height: 60upx;">&nbsp;</p>
		</div>
	</div>
</template>
<script> 
	import serve from '../../static/request.js';
	import common from '../../static/jump.js';
	import ShareIndex from '../../static/share.js';
	export default {
		mixins: [ShareIndex],
		data() {
			return {
				mobileLogin: {
					mobile: '',
					code: '',
					wechatCode: '',
				},
				wechatLogin:{
					code: '',
					encryptedData: '',
					iv: '',
				},
				sendCodeTxt: '获取验证码',
				isSendCode: false,
				isCheckRule: false,
				envVersion: '',
				monitorUse: '',
			};
		},
		methods: {
			login() {
				if (this.mobileLogin.mobile == '') {
					uni.showToast({
						title: '手机号码不能为空',
						icon: 'none'
					});
					return;
				}
				if (this.mobileLogin.code == '') {
					uni.showToast({
						title: '验证码不能为空',
						icon: 'none'
					});
					return;
				}
				if (!this.isCheckRule) {
					uni.showToast({
						title: '请勾选同意服务协议，隐私协议',
						icon: 'none'
					});
					return;
				}
				var self = this;
				uni.login({
					success: function (res) {
						self.mobileLogin.wechatCode = res.code;
						serve.request({
								url: '/site/mobile-login',
								data: self.mobileLogin
							}).then(res => {
								uni.showToast({
									title: res.message,
									icon: 'none'
								});
								if (res.code == 200) {
									uni.setStorageSync('access-token', res.data.access_token);
									self.prev();
								}
							});
					},
				});
			},
			sendCode() {
				if (this.mobileLogin.mobile == '') {
					uni.showToast({
						title: '请填写手机号码',
						icon: 'none'
					});
					return;
				}
                if (!this.isCheckRule) {
                	uni.showToast({
                		title: '请勾选同意服务协议，隐私协议',
                		icon: 'none'
                	});
                	return;
                }
				if (this.isSendCode != false) {
					return ;
				}
				serve.request({
					url: '/site/sms-code',
					data: {
						appkey: uni.getStorageSync('appkey'),
						mobile: this.mobileLogin.mobile,
						usage: 'login',
					}
				}).then(res => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
					if (res.code == 200) {
						this.isSendCode = true;
						this.countDown(60);
					}
				});
			},
			countDown(timeLenght) {
				let self = this;
				timeLenght--;
				if (timeLenght == 0) {
					self.sendCodeTxt = '获取验证码';
					self.isSendCode = false;
					return;
				}
				self.sendCodeTxt = timeLenght + ' S';
				self.monitorUse = setTimeout(function() {
					self.countDown(timeLenght);
				}, 1000);
			},
			getPhoneNumber(e){
				var self = this;
				self.wechatLogin.iv = encodeURIComponent(e.detail.iv);
				self.wechatLogin.encryptedData = encodeURIComponent(e.detail.encryptedData);
				uni.login({
					success: function (res) {
						console.info(res);
						self.wechatLogin.code = res.code;
						console.info(self.wechatLogin);
						self.wechatLoginFun();
					},
					fail:function(){
						uni.showToast({
							title: '当前微信版本不支持，请升级',
							icon: 'none'
						});
					}
				});
			},
			wechatLoginFun() {
				var self = this;
				if(self.wechatLogin.encryptedData == 'undefined' || self.wechatLogin.iv == 'undefined') {
					return ;
				}
				serve.request({
						url: '/site/wechat-login',
						data: self.wechatLogin
					}).then(res => {
						if (res.code == 200) {
							uni.setStorageSync('access-token', res.data.access_token);
							self.prev();
							return ;
						}
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					});
			},
			obtainImgUrl(imgUrl) {
				return serve.imgUrl()+imgUrl;
			},
			jump(jump_type, jump_link) {
				common.jump(jump_type, jump_link);
			},
			bindCheckRule(type) {
                var self = this;
                uni.showModal({
                	title: '提示',
                	content: '请确定并同意小程序服务协议，隐私协议！',
                    confirmText: "确定",
                    cancelText: "我再想想",
                	success: function (res) {
                		if (res.confirm) {
                			self.isCheckRule = !self.isCheckRule;
                            if(type == 2) {
                                self.sendCode();
                            }
                		} else if (res.cancel) {
                			console.log('用户点击取消');
                		}
                	}
                });
                return false;
			},
			prev(){
				let pages = getCurrentPages();
				if(pages[pages.length - 2]){
					uni.navigateBack({
						delta: 1
					})
				}else{
					common.jump('my');
				}
			}
		},
        onHide() {
            clearTimeout(this.monitorUse);
        },
		onLoad() {
			var self = this;
			
            self.envVersion = uni.getStorageSync('envVersion');
			if (wx.getUserProfile) {
				self.canIUseGetUserProfile = true;
			}
		},
		onShow() {
		}
	};
</script>
<style lang="scss" scoped>
	.login {
		padding-bottom: 80upx;
		.top {
			width: 750upx;
			// height: 390upx;
			position: relative;

			>image {
				width: 750upx;
				height: 390upx;
			}

			.tabbar {
				position: absolute;
				left: 0;
				bottom: 0;
				width: 100%;
				height: 88upx;
				display: flex;
				align-items: center;

				>div {
					flex: 1;
					line-height: 88rpx;
					text-align: center;
					font-size: 28upx;
					font-weight: bolder;
					font-family: Microsoft YaHei UI;
					font-weight: 400;
					color: rgba(#ffffff, 1);
					opacity: 1;
					border-bottom: 5upx #fdfdfd solid;
				}

				.check {
					opacity: 1;
					font-size: 36upx;
					border-bottom: 5upx #ffcc00 solid;
				}
			}
		}

		.content {
			.line {
				display: flex;
				align-items: flex-end;
				padding-left: 40upx;
				margin-top: 50upx;

				&:nth-child(2) {
					margin-top: 50upx;
				}

				label {
					display: block;
					width: 160upx;
					padding-bottom: 20upx;
				}

				p {
					display: flex;
					align-items: flex-end;
					border-bottom: 1px solid #ebecf0;
					padding-bottom: 20upx;
					flex: 1;

					input {
						color: #3c4868;
						flex: 1;
					}

					.placeholder {
						color: rgba(60, 72, 104, 0.2);
					}

					span {
						font-size: 32upx;
						color: #eb5938;
						margin-right: 30upx;
					}
				}
			}
            .selPicker {
                border-bottom: 1px solid #ebecf0;padding-bottom:20rpx;width: 100%;text-align: left;margin-left: 34rpx;
            }

			button {
				width: 640upx;
				height: 100upx;
				line-height: 100upx;
				color: #fff;
				background: rgba(255, 200, 10, 1);
				border-radius: 50upx;
				display: block;
				margin: 60upx auto 30upx;
				font-size: 32upx;

				&::after {
					border: none;
				}
			}

			.other {
				p {
					text-align: center;
					font-size: 28upx;
					color: #3c4868;
				}

				image {
					display: block;
					margin: 30upx auto;
					width: 80upx;
					height: 80upx;
					border-radius: 50%;
				}
                .maskBtn {
                    position: absolute;
                    width: 84%;
                    height: 100rpx;
                    padding: 0 60rpx;
                    z-index: 99;
                }
                text {
                    border-radius: 20rpx;
                    border: 1rpx solid #ffc80a;
                    background: #ffc80a;
                    padding: 20rpx 50rpx;
                    width: 80%;
                }
			}

			.rule {
				display: flex;
				align-content: center;
				justify-content: center;
				font-size: 28upx;
				color: rgba(60, 72, 104, 1);
				line-height: 30upx;
				margin-bottom: 50upx;

				span {
					opacity: 0.5;
					line-height: 50upx;
				}
				view{
					line-height: 50upx;
				}

				i {
					width: 32upx;
					height: 32upx;
					border: 1px solid #666;
					box-sizing: border-box;
					margin-right: 10upx;
					margin-top: 10upx;
					border-radius: 50%;
				}

				.check {
					border: none;
					background: url(../../static/img/check.png);
					background-size: 100% auto;
				}
			}
		}
		
		.radio {
			display: flex;
			align-items: center;
			line-height: 32upx;
			font-size: 32upx;
			color: rgba(60, 72, 104, 1);
					
			.radio_i {
				display: block;
				width: 32upx;
				height: 32upx;
				border: 1px solid rgba(#666, 1);
				border-radius: 50%;
				box-sizing: border-box;
				margin-right: 10upx;
			}
			.radio_i.check {
				border: none;
				background: url(http://app.shanhaiyouyu.com/attachment/xcx/my/check.png);
				background-size: 100% auto;
			}
		}
	}
</style>
